<?php
namespace classes;
class Image {
	/**
	 * 产生验证码图片
	 * 
	 * @param int $type,验证码类型(1:数字(默认),2:字母,3:混合)        	
	 * @param int $length,验证码长度,默认值为4        	
	 * @return void
	 */
	public function verify($type = 1, $length = 4) {
		session_start ();
		$width = 110;
		$height = 36;
		switch ($type) {
			case 1 :
				$string = '0123456789';
				break;
			case 2 :
				$string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			default :
				$string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
		}
		$bgPath = dirname(__FILE__) . '/verify/bgs/';
		$dir = dir($bgPath);
		while(($item = $dir->read()) !== false){
			if($item != '.' && $item != '..' && substr($item,strrpos($item,'.') != '.jpg')){
				$bgsArr[] = $item;
			}
		}
		$bgFile = $bgPath . $bgsArr[array_rand($bgsArr)];
		$bgImage = imagecreatefromjpeg($bgFile);
		$bgWidth = imagesx($bgImage);
		$bgHeight = imagesy($bgImage);
		$_SESSION ['verify'] = $string;
		$string = substr ( str_shuffle ( $string ), 0, $length );
		$image = imagecreatetruecolor ( $width, $height );
		imagecopyresampled($image, $bgImage, 0, 0, 0, 0, $width, $height, $width, $height);
		//$white = imagecolorallocate ( $image, 255, 255, 255 );
		//imagefill ( $image, 0, 0, $white );
		$color = imagecolorallocate ( $image, mt_rand ( 50, 200 ), mt_rand ( 50, 200 ), mt_rand ( 50, 200 ) );
		$x = ceil ( ($width - 9 * $length) / 2 );
		imagestring ( $image, 5, $x, 10, $string, $color );
		header ( 'content-type:image/jpeg' );
		imagejpeg ( $image );
		imagedestroy ( $image );
		imagedestroy ( $bgImage );
	}
	/**
	 *水印效果 
	 */
	public function water(){

	}
	/**
	 *缩略图 
	 */
	public function thumb(){
		
	}
}


